package com.open.rbac.controller.sys;

import java.util.List;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.open.center.common.model.CommonResult;
import com.open.rbac.model.dto.sys.OrganizationDto;
import com.open.rbac.model.vo.sys.OrganizationVo;
import com.open.rbac.model.vo.sys.group.OrgUpdate;
import com.open.rbac.service.sys.IOrganizationService;

/**
 * 组织机构控制器
 *
 * @author Riche's
 * @since 2022/8/25
 */
@Tag(name = "组织机构管理", description = "组织机构控制器")
@ApiResponses(value = {
    @ApiResponse(responseCode = "200", description = "成功",
        content = {@Content(schema = @Schema(implementation = OrganizationDto.class))}),
})
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/v1/sys/organization")
public class OrganizationController {

    private final IOrganizationService organizationService;

    @Operation(summary = "新增组织机构")
    @PostMapping()
    public CommonResult<OrganizationDto> create(@Valid @RequestBody OrganizationVo orgVo) {
        return CommonResult.success(organizationService.create(orgVo));
    }

    @Operation(summary = "更新组织机构")
    @Validated(OrgUpdate.class)
    @PutMapping()
    public CommonResult<OrganizationDto> update(@Valid @RequestBody OrganizationVo orgVo) {
        return CommonResult.success(organizationService.update(orgVo));
    }

    @Operation(summary = "组织机构信息")
    @GetMapping("/{id}")
    public CommonResult<OrganizationDto> get(@PathVariable String id) {
        return CommonResult.success(organizationService.findById(id));
    }

    @Operation(summary = "删除组织机构")
    @DeleteMapping("/{id}")
    public CommonResult<Void> deleteById(@PathVariable String id) {
        organizationService.deleteById(id);
        return CommonResult.success();
    }

    @Operation(summary = "启用|禁用组织机构")
    @PutMapping("/enable/{id}")
    public CommonResult<Void> enable(@PathVariable String id,
                                     @RequestParam(required = false) Boolean enable) {
        if (Boolean.TRUE.equals(enable)) {
            organizationService.enable(id);
        } else {
            organizationService.disable(id);
        }
        return CommonResult.success();
    }

    @Operation(summary = "组织机构树")
    @PostMapping("/tree")
    public CommonResult<List<OrganizationDto>> tree(@RequestBody OrganizationVo organizationVo) {
        return CommonResult.success(organizationService.tree(organizationVo));
    }

    @Operation(summary = "查询Id及其子节点Id列表")
    @GetMapping("/list_child_ids/{id}")
    public CommonResult<List<String>> listChildIdsById(@PathVariable String id) {
        return CommonResult.success(organizationService.listChildIdsById(id));
    }
}
